package com.ankicoo.auth.relam;

import com.ankicoo.auth.entity.AuthUserPO;
import com.ankicoo.auth.mapper.UserMapper;
import com.ankicoo.auth.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Description: 用户权限数据域
 *
 * @author : QCYANG
 * @date : 2021/1/11 09:50
 */
public class CustomerRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
         String username = ((UsernamePasswordToken) token).getUsername();
        AuthUserPO user = userService.findUserInfoByUserId(username);
        if(user == null) {
            throw new UnknownAccountException("账号不存在");
        }
        return new SimpleAuthenticationInfo(
                username,
                user.getPassword(),
                ByteSource.Util.bytes(user.getSalt()),
                this.getClass().getSimpleName()
        );
    }
}
